layui.define(['laypage', 'layer',  'table','common','util','form'], function (exports) {
    var $ = layui.jquery,
        layer = layui.layer,
        util = layui.util,
        laypage = layui.laypage,
        form = layui.form,
        common = layui.common,
        table  = layui.table ;
    table.render({
        elem: '#course'
        ,height: 'full-200'
        ,method:'GET'
        ,url: '/admin/course/list' //数据接口
        ,page: true //开启分页
        ,cols: [[ //表头
            {type: 'checkbox', align:'center',unresize:true}
            ,{field: 'num', align:'center', title: '课程编号',width: '5%',unresize:true}
            ,{field: 'name', align:'center', title: '课程名称',width: '10%',unresize:true}
            ,{field: 'dept', align:'center', title: '所属院系',width: '10%',unresize:true,templet:'<div>{{d.dept.name}}</div>'}
            ,{field: 'beginWeek', align:'center', title: '开始周',width: '5%',unresize:true}
            ,{field: 'endWeek', align:'center', title: '结束周',width: '5%',unresize:true}
            ,{field: 'week', align:'center', title: '星期',width: '5%',unresize:true}
            ,{field: 'section', align:'center', title: '节数',width: '10%',unresize:true}
            ,{field: 'teacher', align:'center', title: '教师',width: '8%',unresize:true,templet:'<div>{{d.teacher.name}}</div>'}
            ,{field: 'createDate',align:'center', title: '创建日期',width: '15%',unresize:true}
            ,{field: 'personTime', align:'center',title: '累计人次',width: '7%',unresize:true}
            ,{fixed: 'right',  title:'操作',align:'center', toolbar: '#operator',width: '20%',unresize:true}
        ]]
    });
    //监听工具条
    table.on('tool(table)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            del(data.id);
        } else if(obj.event === 'edit'){
            common.frame_show('分类编辑','/admin/course/form?id='+data.id);
        }
    });
    //分页
    laypage.render({
        elem: 'pageDemo' //分页容器的id
        ,count: 100 //总页数
        ,skin: '#1E9FFF' //自定义选中色值
        //,skip: true //开启跳页
        ,jump: function(obj, first){
            if(!first){
                layer.msg('第'+ obj.curr +'页');
            }
        }
    });


    //添加数据
    $('#addDept').click(function () {
        var index = layer.load(1);
        setTimeout(function () {
            layer.close(index);
            common.frame_show('分类添加','/admin/course/form');
            // layer.msg('打开添加窗口');
        }, 500);
    });

    //批量删除数据
    $('#deleteAll').click(function () {
        var index = layer.load(1);

    });

    //导出数据
    $('#exportExcel').click(function (){
        var index = layer.load(1);
        setTimeout(function () {
            layer.close(index);
            layer.msg('正在拼命为您导出清单，请稍后。。。');
            window.location.href = "/admin/course/exportExcel"
        }, 500);
    });

    //输出接口，主要是两个函数，一个删除一个编辑
    var datalist = {
        deleteData: function (id) {
            layer.confirm('确定删除？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                del(id);
            }, function () {

            });
        },
        editData: function (id) {
            common.frame_show('分类编辑','/admin/course/form?id='+id);
        }
    };
    function del(id) {
        layer.confirm('真的删除行么', function (index) {
            $.ajax({
                type: "DELETE",
                dataType: "json",
                url: "/admin/course/" + id + "/del",
                success: function (ret) {
                    if (ret.isOk) {
                        layer.msg("操作成功", {time: 2000}, function () {
                            layer.close(index);
                            window.location.href = "/admin/course/index";
                        });
                    } else {
                        layer.msg(ret.msg, {time: 2000});
                    }
                }
            });
        });
    }


    exports('course/index', datalist);
});